import Rsync from "rsync";
import { resolve } from "node:path";
import { readJSONSync, pathExistsSync } from "fs-extra/esm";
import { getCurrentBranchName } from "./currentBranch.js";

const currentBranch = getCurrentBranchName();
if (currentBranch !== "master") {
  console.error('请切换到 "master" 分支');
  process.exit(-1);
}

const __dirname = resolve();

const dist = resolve(__dirname, ".vitepress/dist");

if (!pathExistsSync(dist)) {
  console.error(`${dist} 不存在`);
  process.exit(-1);
}

const name = readJSONSync(resolve(__dirname, "package.json")).name;

// rsync -avrz --delete .vitepress/dist root@0230812.xyz:/usr/local/kaijia-web/knowledge-base/
const rsync = new Rsync()
  .shell("ssh")
  .flags("avrz")
  .delete()
  .source(dist)
  .destination(`root@0230812.xyz:/usr/local/kaijia-web/${name}`);

rsync.execute((error, code, cmd) => {
  if (error) {
    console.log("出错了");
    console.error(error);
    console.log(code);
  } else {
    console.log("上传成功");
  }
});
